local PRE_SFX_VOLUME = 0.7

---主机，抬枪sg，进入攻击sg
local homura_gun_pre = State{
    name = "homura_gun_pre",
    tags = {"attack", "notalking", "abouttoattack", "autopredict",'homura_gun_pre'},
        
    onenter = function(inst)
        local buffaction = inst:GetBufferedAction()
        local target = buffaction and buffaction.target
        local weapon = inst.components.combat:GetWeapon()

        inst.components.locomotor:Stop()

        --inst.components.combat:CancelAttack()
       
        inst.AnimState:PlayAnimation("homura_gun_pre")
        inst.SoundEmitter:PlaySound('lw_homura/sfx/load_down_3d', nil, PRE_SFX_VOLUME, true)

        if target and target:IsValid() then
            inst:FacePoint(target.Transform:GetWorldPosition())
        end

        inst.sg.statemem.weapon = weapon
    end,

    timeline =
    {
        TimeEvent(12*FRAMES, function(inst)
            inst.sg.statemem.gotoatk = true
            local weapon = inst.sg.statemem.weapon
            if not weapon then
                inst.sg:GoToState('idle')
            elseif weapon.prefab == 'homura_gun' then
                inst.sg:GoToState('homura_gun_atk')
            elseif weapon.prefab == 'homura_hmg' then
                inst.sg:GoToState('homura_hmg_atk')
            end
        end),
    },

    events = {
        CommonAnimover(),
        CommonEquip(),
        CommonUnequip(),
    },

    onexit = function(inst)
        if not inst.sg.statemem.gotoatk then
            inst:ClearBufferedAction()
        end
    end,
}

AddStategraphState("wilson", homura_gun_pre)




-- 客机，抬枪sg，只播放动画和声音
local homura_gun_pre_client = State{
    name = "homura_gun_pre",
    tags = {"attack", "notalking", "abouttoattack", "homura_gun_pre"},
        
    onenter = function(inst)
        local buffaction = inst:GetBufferedAction()
        local target = buffaction and buffaction.target
        if buffaction ~= nil then
            inst:PerformPreviewBufferedAction()
        end
        if target ~= nil and target:IsValid() then
            inst:FacePoint(target.Transform:GetWorldPosition())
        end

        inst.sg.statemem.weapon = inst.replica.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)

        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("homura_gun_pre")
        inst.SoundEmitter:PlaySound('lw_homura/sfx/load_down_3d',nil, PRE_SFX_VOLUME, true)

        inst.sg:SetTimeout(1) 
    end,

    timeline =
    {
        TimeEvent(12*FRAMES, function(inst)
            inst.sg.statemem.gotoatk = true
            local weapon = inst.sg.statemem.weapon
            if not weapon then
                inst.sg:GoToState('idle')
            elseif weapon.prefab == 'homura_gun' then
                inst.sg:GoToState('homura_gun_atk')
            elseif weapon.prefab == 'homura_hmg' then
                inst.sg:GoToState('homura_hmg_atk')
            end
        end),
    },


    ontimeout = function(inst)
        inst:ClearBufferedAction()
        inst.sg:GoToState("idle")
    end,
}

AddStategraphState('wilson_client', homura_gun_pre_client)



--后摇
local homura_gun_pst = State{
    name = 'homura_gun_pst',
    tags = {"notalking"},

    onenter = function(inst)
        print('go to pst')
        inst.AnimState:PlayAnimation('homura_gun_pst') 
        inst.sg:SetTimeout(5/30)
    end,

    ontimeout = function(inst) inst.sg:GoToState('idle')end,
}

AddStategraphState('wilson', homura_gun_pst)

local homura_gun_pst_client = State{
    name = 'homura_gun_pst',
    tags = {"notalking"},

    onenter = function(inst)
        inst.AnimState:PlayAnimation('homura_gun_pst')
        inst.sg:SetTimeout(5/30)
    end,

    ontimeout = function(inst) inst.sg:GoToState('idle')end,

}

AddStategraphState('wilson_client', homura_gun_pst_client)

